1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
| #include <bits/stdc++.h> #define rep(i, x, y) for (int i = x; i <= y; i++) using namespace std;
const int N = 505, M = 205, E = N * M * 2 + N + M, inf = 0x3f3f3f3f; int S, T, n, m, Cost, V; int a[N], b[M], mp[N][N], ans[N]; int fr[E], to[E], nxt[E], lnk[N * 2], val[E], cap[E], cnt = 1; int dis[N * 2], pre[N * 2], inq[N * 2], rest[N * 2]; queue<int> q;
void add(int x, int y, int c, int w) { fr[++cnt] = x, to[cnt] = y, nxt[cnt] = lnk[x], lnk[x] = cnt, val[cnt] = w, cap[cnt] = c; }
bool spfa(int S, int T, int &flow, int &cost) { rep(i, S, T) { dis[i] = inf; pre[i] = 0; inq[i] = 0; } dis[S] = 0, inq[S] = 1, rest[S] = inf, pre[S] = 0; q.push(S); while (q.size()) { int x = q.front(); q.pop(); inq[x] = 0; for (int i = lnk[x]; i; i = nxt[i]) { int y = to[i]; if (cap[i] && dis[y] > dis[x] + val[i]) { dis[y] = dis[x] + val[i]; pre[y] = i; rest[y] = min(rest[x], cap[i]); if (!inq[y]) inq[y] = 1, q.push(y); } } } if (dis[T] > 1e9) return 0; flow += rest[T]; cost += rest[T] * dis[T]; int u = T; while (u != S) { cap[pre[u]] -= rest[T]; cap[pre[u] ^ 1] += rest[T]; u = fr[pre[u]]; } return 1; }
int Mcmf() { int flow = 0, cost = 0; while (spfa(S, T, flow, cost)); return cost; }
int main() { cin >> n >> m; S = 0, T = n + m + 1; rep(i, 1, n) { scanf("%d", &a[i]); } V = a[2]; rep(i, 1, m) { scanf("%d", &b[i]); } rep(i, 1, n) { rep(j, 1, m) { scanf("%d", &mp[i][j]); } } b[48] -= a[1]; rep(i, 2, n) { add(S, i, 1, 0), add(i, S, 0, 0); } rep(i, 1, m) { add(i + n, T, b[i] / V, 0), add(T, i + n, 0, 0); } rep(i, 2, n) { rep(j ,1, m) { add(i, j + n, 1, -mp[i][j]), add(j + n, i, 0, mp[i][j]); } } int cost = Mcmf() - mp[1][48]; ans[1] = 48; rep(x, 2, n) { ans[x] = 0; for (int i = lnk[x]; i; i = nxt[i]) { int y = to[i]; if (!y) continue; if (!cap[i]) { ans[x] = y - n; break; } } } rep(x, 1, n) printf("%d ", ans[x]); puts(""); return 0; }
|